package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.entity.Information;
import net.qhzw.vip.qiyu.entity.Shop;
import net.qhzw.vip.qiyu.entity.ShopAccount;
import net.qhzw.vip.qiyu.entity.Withapply;
import net.qhzw.vip.qiyu.feign.ShopFeignClient;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.mapper.WithapplyMapper;
import net.qhzw.vip.qiyu.service.InformationService;
import net.qhzw.vip.qiyu.service.ShopAccountService;
import net.qhzw.vip.qiyu.service.WithapplyService;
import net.qhzw.vip.qiyu.util.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;


@Service
public class WithapplyServiceImpl extends ServiceImpl<WithapplyMapper, Withapply> implements WithapplyService {

    @Autowired
    private WithapplyMapper withapplyMapper;

    @Autowired
    private InformationService informationService;

    @Autowired
    private ShopFeignClient shopFeignClient;

    @Autowired
    private ShopAccountService shopAccountService;


    @Override
    public IPage<Withapply> findPage(PageParam<Withapply> page, Withapply withapply) {
        Shop shop = shopFeignClient.findById(withapply.getShopId()).getData();
        if (shop == null) {
            throw new ApiException("商户信息有误!");
        }


        IPage<Withapply> pageInfo = page(page, new LambdaQueryWrapper<Withapply>()
                .eq(shop.getShopType() != 0, Withapply::getShopId, withapply.getShopId()).last("ORDER BY `status`, created DESC"));
        List<Withapply> records = pageInfo.getRecords();


        if (!CollectionUtils.isEmpty(records)) {
            List<Information> list = informationService.list(new LambdaQueryWrapper<Information>().eq(withapply != null && withapply.getShopId() != 1, Information::getShopId, withapply.getShopId()));
            if (!CollectionUtils.isEmpty(list)) {
                for (Withapply record : records) {
                    for (Information information : list) {
                        if (record.getShopId().equals(information.getShopId())) {
                            record.setShopInfo(information);
                        }
                    }
                }
            }
        }
        return pageInfo;
    }


    @Override
    @Transient
    public boolean payment(Withapply withapply) {
        ShopAccount shopAccount = new ShopAccount();
        shopAccount.setTransferAmount(withapply.getAmount());
        shopAccount.setTransactionType(0);
        shopAccount.setShopId(withapply.getShopId());
        shopAccount.setRemark("提现");
        Shop data = shopFeignClient.findById(withapply.getShopId()).getData();
        if (data == null || data.getShopId() == null) {
            throw new ApiException("该商户不存在，请核实信息！");
        }
        shopAccount.setShopName(data.getShopName());
        shopAccountService.save(shopAccount);
        informationService.income(withapply.getShopId(), -withapply.getAmount());
        withapply.setStatus(3);
        updateById(withapply);
        return false;
    }
}
